const {Controller}=require("egg")

const {createId}=require("../utils/utils")

class interfaceController extends Controller{

    // 获取;
    async index(){
        const {ctx}=this
        const result=await ctx.service.interface.index()
        if(result.length){
            ctx.body={
                code:"1001",
                msg:"接口数据成功",
                result
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"接口数据失败"
            }
        }
    }

    // 查看
    async show(){
        const {ctx}=this
        const {id}=ctx.params
        const result=await ctx.service.interface.show(id)
        if(result.length){
            ctx.body={
                code:"1001",
                msg:"接口查看成功",
                result
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"接口查看失败"
            }
        }
    }

    // 编辑
    async update(){
        const {ctx}=this
        const {id}=ctx.params
        const {interface_text,methods,interface_url}=ctx.request.body
        const result=await ctx.service.interface.update(id,interface_text,methods,interface_url)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"接口编辑成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"接口编辑失败"
            }
        }
    }

    // 删除
    async destroy(){
        const {ctx}=this
        const {id}=ctx.params
        const result=await ctx.service.interface.destroy(id)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"接口删除成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"接口删除失败"
            }
        }
    }

    // 增加
    async create(){
        const {ctx}=this
        const {interface_text,methods,interface_url}=ctx.request.body
        const id=createId(interface_text)
        const result=await ctx.service.interface.create(id,interface_text,methods,interface_url)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"接口增加成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"接口增加失败"
            }
        }
    }
}

module.exports=interfaceController